package mya.mes.mdc.logic.dispatch.core;

import mya.mes.mdc.local.alarm.MdcAlarmResult;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class AlarmDispatchManager {


    private static AlarmDispatchManager _instance = new AlarmDispatchManager();
    private ScheduledExecutorService executorService = null;


    private final AlarmCurrent alarmCurrent = new AlarmCurrent();

    private AlarmDispatchManager() {

    }

    public static AlarmDispatchManager getInstance() {
        return _instance;
    }


    public synchronized void start() throws Exception {
        if (executorService == null) {
            executorService = Executors.newScheduledThreadPool(1);
            alarmCurrent.updateCache();
            executorService
                    .scheduleWithFixedDelay(new AlarmTimerTask(),
                            1L, 5L, TimeUnit.SECONDS);
        }

    }


    public void updateCachedAlarm(MdcAlarmResult alarmResult) throws Exception {
        alarmCurrent.update(alarmResult);
        /// alarmDispatchService.build(alarmResult);
    }

    AlarmCurrent getAlarmCurrent() {
        return alarmCurrent;
    }

    @Override
    protected void finalize() throws Throwable {

        if (executorService != null) {
            executorService.shutdown();
        }
        super.finalize();
    }
}
